import { MessageType } from '@/enums/event'

/** content向popup发送消息 */
function sendMessageContentToPopup(data: any) {
  chrome.runtime?.sendMessage({
    type: MessageType.CONTENT_TO_POPUP,
    data,
  })
}

/** content接收popup消息 */
function receiveMessageContentFromPopup(callback: (data: any) => void) {
  chrome.runtime.onMessage.addListener((event) => {
    const { type, data } = event.data || {}
    if (type === MessageType.POPUP_TO_CONTENT) {
      callback(data)

      return true
    }
    return false
  })
}

/** 从popup向content发送消息 */
function sendMessagePopupToContent(data: any, callback?: (data: any) => void) {
  chrome.tabs.query({ active: true, currentWindow: true }, (tabs: any) => {
    chrome.tabs.sendMessage(
      tabs[0].id,
      {
        type: MessageType.POPUP_TO_CONTENT,
        data,
      },
      callback || (() => {}),
    )
  })
}

/** popup接收content消息 */
function receiveMessagePopupFromContent(callback: (data: any) => void) {
  chrome.runtime.onMessage.addListener((event) => {
    const { type, data } = event.data || {}
    if (type === MessageType.CONTENT_TO_POPUP) {
      callback(data)

      return true
    }
    return false
  })
}

/** content向service-worker发送消息 */
function sendMessageContentToServiceWorker(data: any) {
  if (navigator?.serviceWorker?.controller) {
    navigator.serviceWorker.controller.postMessage({
      type: MessageType.CONTENT_TO_SERVICE_WORKER,
      data,
    })
  }
  else {
    console.error('当前环境不支持content向Service Worker发送消息，navigator.serviceWorker.controller不存在。')
  }
}

/** content接收service-worker的消息 */
function receiveMessageContentFromServiceWorker(callback: (data: any) => void) {
  navigator.serviceWorker.addEventListener('message', (event) => {
    const { type, data } = event.data || {}
    if (type === MessageType.SERVICE_WORKER_TO_CONTENT) {
      callback(data)
    }
  })
}

/** service-worker向content发送消息 */
function sendMessageServiceWorkerToContent(data: any) {
  if (navigator?.serviceWorker?.controller) {
    navigator.serviceWorker.controller.postMessage({
      type: MessageType.SERVICE_WORKER_TO_CONTENT,
      data,
    })
  }
  else {
    console.error('当前环境不支持Service Worker向content发送消息，navigator.serviceWorker.controller不存在。')
  }
}

/** service-worker接收content的消息 */
function receiveMessageServiceWorkerFromContent(callback: (data: any) => void) {
  console.log('serviceWorker>>>>', navigator.serviceWorker)
  navigator.serviceWorker?.addEventListener('message', (event) => {
    const { type, data } = event.data || {}
    if (type === MessageType.CONTENT_TO_SERVICE_WORKER) {
      callback(data)
    }
  })
}

/** popup向service-worker发送消息 */
function sendMessagePopupToServiceWorker(data: any, callback: (data: any) => void) {
  chrome.runtime.sendMessage(
    {
      type: MessageType.POPUP_TO_SERVICE_WORKER,
      data,
    },
    callback,
  )
}

/** service-worker向popup发送消息 */
function sendMessageServiceWorkerToPopup(data: any, callback: (data: any) => void) {
  chrome.runtime.sendMessage({
    type: MessageType.SERVICE_WORKER_TO_POPUP,
    data,
  }, callback)
}

/** popup接收service-worker的消息 */
function receiveMessageServiceWorkerToPopup(callback: (data: any) => void) {
  chrome.runtime.onMessage.addListener((message) => {
    if (message.type === MessageType.SERVICE_WORKER_TO_POPUP) {
      callback(message.data)
    }
    return true
  })
}

/** service-worker接收popup的消息 */
function receiveMessageServiceWorkerFromPopup(callback: (data: any) => void) {
  chrome.runtime.onMessage.addListener((message) => {
    if (message.type === MessageType.SERVICE_WORKER_TO_POPUP) {
      callback(message.data)
    }
    return true
  })
}

export {
  receiveMessageContentFromPopup,
  receiveMessageContentFromServiceWorker,
  receiveMessagePopupFromContent,
  receiveMessageServiceWorkerFromContent,
  receiveMessageServiceWorkerFromPopup,
  receiveMessageServiceWorkerToPopup,
  sendMessageContentToPopup,
  sendMessageContentToServiceWorker,
  sendMessagePopupToContent,
  sendMessagePopupToServiceWorker,
  sendMessageServiceWorkerToContent,
  sendMessageServiceWorkerToPopup,
}
